/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainView.java
 *
 * Created on 21.Tem.2011, 20:47:21
 */

package View;

import Controller.*;
import javax.swing.JOptionPane;
import Database.Manager;

/**
 *
 * @author Sinem
 */
public class MainView extends javax.swing.JFrame {

    URLController m_urlController;
    SpiderController m_spiderController;
    RuleController m_ruleController;
    /** Creates new form MainView */
    public MainView() {
        initComponents();
        //initialize the controllers
        m_urlController = new URLController();
        m_spiderController = new SpiderController();
        m_ruleController = new RuleController();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCheck = new javax.swing.JButton();
        txtURL = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnCheck.setText("Check");
        btnCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckActionPerformed(evt);
            }
        });

        txtURL.setColumns(35);
        txtURL.setToolTipText("Address of Web Page tol be checked");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(txtURL, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(79, 79, 79)
                .addComponent(btnCheck)
                .addContainerGap(142, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCheck))
                .addContainerGap(141, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckActionPerformed
        String sInputURL = "";
        boolean bURLValid = false;        
        boolean bRuleCheckCompleted = false;
        sInputURL = txtURL.getText();

        bURLValid = m_urlController.checkURL(sInputURL);

        if (bURLValid == false)
        {           
            JOptionPane.showMessageDialog(this, m_urlController.getErrMsg(), "ERROR",
                        JOptionPane.ERROR_MESSAGE);
        }
        else
        {            
            /*
             Downloading all web site is a long process
             so all web sites are already downloaded by calling wget from a script
             if wget is needed to be integrated into the code, activate the code 
             block below
            */           
           try{
           //db manager will be created and the url will be set
           Manager.getInstance().setWebSite(sInputURL);
           //download is already performed. Check all rules
           sInputURL = "I:\\Thesis\\SelectedWebSiteResults\\" + sInputURL;
           bRuleCheckCompleted = m_ruleController.checkAllFiles(sInputURL);
           if (bRuleCheckCompleted)
            JOptionPane.showMessageDialog(this, "Rule Check is completed","INFORMATION",
                        JOptionPane.INFORMATION_MESSAGE);
           }catch(IllegalMonitorStateException e){e.printStackTrace();}                   
        }
}//GEN-LAST:event_btnCheckActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheck;
    private javax.swing.JTextField txtURL;
    // End of variables declaration//GEN-END:variables

}
